package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.elasticsearch.query.SearchQueryObject;
import cn.wolfcode.wolf2w.elasticsearch.service.ISearchService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
public class SearchController {
	@Autowired
	private ISearchService searchService;

	@GetMapping("q")
	public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
		qo.setKeyword(URLDecoder.decode(qo.getKeyword(),"UTF-8"));

		switch (qo.getType()){
			case SearchQueryObject.TYPE_DEST:
				return JsonResult.success(searchService.searchDest(qo));
			case SearchQueryObject.TYPE_STRATEGY:
				return JsonResult.success(searchService.searchStrategy(qo));
			case SearchQueryObject.TYPE_TRAVEL:
				return JsonResult.success(searchService.searchTravel(qo));
			case SearchQueryObject.TYPE_USER:
				return JsonResult.success(searchService.searchUser(qo));
			default:
				return JsonResult.success(searchService.searchAll(qo));
		}
	}
}
